package com.test

import com.alibaba.fastjson.JSON
import org.apache.calcite.avatica.org.apache.http.client.methods.HttpGet
import org.apache.calcite.avatica.org.apache.http.impl.client.HttpClients
import org.apache.calcite.avatica.org.apache.http.util.EntityUtils

import java.io.BufferedInputStream

/**
 * @Date: 23.6.6 
 * @Author: Hang.Nian.YY
 * @qq: 598196583
 * @Tips: 学大数据 ,到多易教育
 * @Description:
 * 使用代码请求网络API
 * 接收返回的数据
 * 解析数据
 */
object TestHttpClient {
  def main(args: Array[String]): Unit = {
    // 创建客户端对象
    val client = HttpClients.createDefault()
    // 创建请求对象
    val httpGet = new HttpGet("https://api.map.baidu.com/reverse_geocoding/v3/?ak=18i6fQLQURn2RRPM8XSO1qH65OS2MXRD&output=json&coordtype=wgs84ll&location=32.05940854500833,98.96748094858353")
    // 发送请求  接收数据
    val response = client.execute(httpGet)
    // 获取返回数据的内容
    val content = response.getEntity
    // 解析内容
    val str = EntityUtils.toString(content)
		val dataObject = JSON.parseObject(str)
		val addressObject = dataObject.getJSONObject("result").getJSONObject("addressComponent")

		val province = addressObject.getString("province")
		val city = addressObject.getString("city")
		val district = addressObject.getString("district")

		println(s"$province $city  $district")
  }

}
